computeStepFunction <- function(x,resultsFiles){
  #print(length(x))
  print(paste("[STATUS] - Cluster",x))
  # Extraction
  clusterVisitingTimes <- lapply(resultsFiles,extractClusterWaitingTimes,clusterId=x)
  print("[STATUS] - Stretching data")
  # Stretching data on the same vector length
  clusterVisitingTimes <- lapply(clusterVisitingTimes, function(x) return(expandWaitingTimes(x[,1],x[,2],10000)))
  #Framing
  clusterVisitingTimes <- data.frame(matrix(unlist(clusterVisitingTimes), ncol=length(clusterVisitingTimes)))
  print("[STATUS] - Computing quantiles & mean")
  #Quantiles
  quantilesVisiting <- t(apply(clusterVisitingTimes,1,quantile))
  #Mean
  meanVisiting <- apply(clusterVisitingTimes,1,mean)
  #Return median
  print(max(quantilesVisiting[,3]))
  return(quantilesVisiting[,3])
  #Return mean
  #return(meanVisiting)
  
  
  #Ordering
  #clusterVisitingTimes <- clusterVisitingTimes[order(clusterVisitingTimes[,1]),]
  # Summing
  #clusterVisitingTimes[,2] <- cumsum(clusterVisitingTimes[,2])
  # Step function
  #print(clusterVisitingTimes[length(clusterVisitingTimes[,2]),2])
  #return(stepfun(c(clusterVisitingTimes[,1],10000),c(0,clusterVisitingTimes[,2],clusterVisitingTimes[length(clusterVisitingTimes[,2]),2])))
}

extractClusterWaitingTimes <- function(inputFile,clusterId){
  inputData <- read.table(inputFile, header = TRUE, sep = "\t")
  times <- inputData[inputData[,3]==clusterId,1]
  robots <- inputData[inputData[,3]==clusterId,2]
  return(cbind(times,cumsum(robots)))
}

# Function to convert waiting times
expandWaitingTimes <- function(x,y,length){
  differences <- c(diff(x),length - x[length(x)])
  yExt <- c(rep(0,x[1]+1),unlist(sapply(seq(length(y)),function(x) rep(y[x],differences[x]))))
  return(yExt)
}

################################################################################################################
library(extrafont)

# $1 - Directory where the .occ files corresponding to the different experiments are stored
args <- commandArgs(trailingOnly = TRUE)
workingDirectory <- args[1]
rm(args)

#Set the directory containing the experiment results as current working directory
setwd(paste("/home/deste/argos2/user/jdestefani/results/",workingDirectory,"/",sep=""))
#List all the files containing information about occupation
resultsFiles <- list.files(pattern = "\\.vt$")
clustersId <- seq(from=0,to=3)


print("[STATUS] - Processing results file")
printData <- lapply(clustersId,computeStepFunction,resultsFile=resultsFiles)
maxVal <- max(unlist(printData))
print("[STATUS] - Computing step functions")
#print(printData)

print("[STATUS] - Producing plots")
lineTypes <- c(1,2,4,5)
colors <- c("black","grey25","grey50","grey75")
pdf(paste(workingDirectory,"clusters","pdf",sep="."),family="Droid Sans")
par(bty="l")
plot(range(0,10*as.numeric(1000)),range(0,1.2*maxVal),type="n",xlab="Time steps",ylab="Cluster observations",main=paste("Distribution of visit times for each cluster across 50 trials"))
lapply(seq(length(printData)),FUN=function(x) lines(printData[[x]],col=colors[x],lty=lineTypes[x],pch="."))
#lapply(seq(length(printData)),FUN=function(x) plot(printData[[x]],add=TRUE,col=colors[x],lty=lineTypes[x],pch="."))
legend(x="bottomright", legend=(clustersId+1) , horiz=TRUE, cex=0.8, col=colors, lty=lineTypes, title="Clusters")
dev.off()


#pdf("Boxplots.pdf")
#boxplot(WaitingTimes ~ ClusterId,data=waitingTimes)
#dev.off()